/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import model.Base;
import model.Person;
import model.StaffMember;
import model.Student;
import model.Teacher;
import view.UserPropertiesView;

/**
 *
 * @author Ira
 */
public class UserPropertiesController implements ActionListener {

	private Base model;
	private UserPropertiesView view;
	
	public UserPropertiesController(Base model, UserPropertiesView view) {
		this.model = model;
		this.view = view;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String name = ((JButton)e.getSource()).getName();
		if(name.equals(UserPropertiesView.BTN_DELETE)) {
			if(this.model.isUserModifiable(this.view.getUser())) {
				this.model.UserDelete(this.view.getUser());
				((JButton)e.getSource()).setVisible(false);
			}
		}
		else if(name.equals(UserPropertiesView.BTN_SAVE)) {
			if(!this.model.isUserModifiable(this.view.getUser())) {
				return;
			}
			
			Person newPerson = null;
			String type = this.view.getTxtType();
			
			if(type.equals("Etudiant")) {
				newPerson = new Student(this.view.getTxtName(), this.view.getTxtFirstName(), this.view.getTxtPseudo(), this.view.getUser().getPass());
			}
			else if(type.equals("Professeur")) {
				newPerson = new Teacher(this.view.getTxtName(), this.view.getTxtFirstName(), this.view.getTxtPseudo(), this.view.getUser().getPass());
			}
			else if(type.equals("Bibliothécaire")) {
				newPerson = new StaffMember(this.view.getTxtName(), this.view.getTxtFirstName(), this.view.getTxtPseudo(), this.view.getUser().getPass());
			}
			this.model.UserMaj(this.view.getUser(), newPerson);
		}
	}
	
}
